package com.samoilovich.belarustoday.basefragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.adapters.CategoryItemsAdapter;
import com.samoilovich.belarustoday.datemodel.CategoryModel;
import com.samoilovich.belarustoday.datemodel.NewsModel;
import com.samoilovich.belarustoday.handlers.NewsHandler;
import com.samoilovich.belarustoday.tools.Utility;

import java.util.ArrayList;

public class NewsListBaseFragment extends BaseFragment implements AbsListView.OnScrollListener{
    private View mFooter;

    protected ArrayList<NewsModel> sNews;
    protected CategoryItemsAdapter mAdapter;
    protected ListView mListView;
    protected CategoryModel mCurrentCategory;
    protected ProgressBar mProgressBarContentWait;
    protected View mViewNoConnection;
    protected int mBackLabel;
    protected AdapterView.OnItemClickListener mOnItemClickListener;
    protected boolean mInProgress;

    public NewsListBaseFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_main, null);
        prepareActionBarViews(mCurrentCategory.getName(), mBackLabel, true);
        prepareViews();

        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (sNews != null) {
            updateScreen();
        }
    }

    @Override
    public void updateCategoryContent(final ArrayList<NewsModel> news) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mFooter.setVisibility(View.GONE);
                sNews = news;
                updateScreen();
                mInProgress = false;
            }
        });
    }

    @Override
    public void onReceiveInternetIntent(boolean hasInternetConnection, Intent intent) {
        if (mListView == null || mProgressBarContentWait == null || mAdapter == null) {
            prepareViews();
        }
        if (intent.getAction().equals(Constants.PICTURE_LOADING_ACTION)) {
            if (mAdapter != null) {
                mAdapter.setPictureLoadingState(Utility.getPictureLoadingState(getActivity()));
            }
        } else {
            if (hasInternetConnection) {
                mViewNoConnection.setVisibility(View.GONE);
                mFooter.setVisibility(View.VISIBLE);
                if (!mInProgress && sNews == null) {
                    getNews(getActivity());
                }
            } else {
                mFooter.setVisibility(View.GONE);
                if (sNews == null) {
                    mViewNoConnection.setVisibility(View.VISIBLE);
                }
            }
        }
    }


    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) { }

    @Override
    public void onScroll(AbsListView absListView, int firstVisiblePosition, int countOfVisibleItems, int totalCount) {
        if (totalCount > 0 && countOfVisibleItems > 0) {
            int position = totalCount - firstVisiblePosition - countOfVisibleItems;
            if (position <= Constants.AUTOLOADING_SCHEDULE && !mInProgress && Utility.hasInternetConnection(getActivity())) {
                mInProgress = true;
                mFooter.setVisibility(View.VISIBLE);
                NewsModel lastNews = sNews.get(sNews.size() - 1);
                NewsHandler.getInstance().getMoreCategoryNews(NewsListBaseFragment.this, mCurrentCategory.getId(), "10", lastNews.getTime());
            }
        }
    }

    @Override
    public void updateAllData(Context context) {
        updateNews();
        getNews(context);
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    protected void prepareViews() {
        mFooter = getLayoutInflater(null).inflate(R.layout.progresslbar_text1, null);

        mProgressBarContentWait = (ProgressBar) mRootView.findViewById(R.id.progressBar_categoryWait);
        mListView = (ListView) mRootView.findViewById(R.id.listView_category);
        mViewNoConnection = mRootView.findViewById(R.id.view_noConnection);
        mAdapter = new CategoryItemsAdapter(getActivity(), null, R.layout.category_content_list_item, mCurrentCategory);
        if (mFooter != null) {
            mListView.addFooterView(mFooter);
        }
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(mOnItemClickListener);
    }

    protected void updateNews() {
        if (mListView == null || mProgressBarContentWait == null || mAdapter == null) {
            prepareViews();
        }
        mListView.setVisibility(View.GONE);
        mViewNoConnection.setVisibility(View.GONE);
        mProgressBarContentWait.setVisibility(View.VISIBLE);
        sNews = null;
    }

    protected void getNews(Context context) {
        if (Utility.hasInternetConnection(context)) {
            mInProgress = true;
            NewsHandler.getInstance().getCategoryNewsList(this, mCurrentCategory.getId());
        }
    }

    // -----------------------------------------------------------------------------------
    private void updateScreen() {
        if (mListView == null || mProgressBarContentWait == null || mAdapter == null) {
            prepareViews();
        }
        mListView.setVisibility(View.VISIBLE);
        mListView.setOnScrollListener(this);
        mProgressBarContentWait.setVisibility(View.GONE);
        mViewNoConnection.setVisibility(View.GONE);
        mFooter.setVisibility(View.VISIBLE);
        mAdapter.setNews(sNews);
    }
}
